package com.ipad.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ipad.R;
import com.ipad.bean.Delivery;
import com.ipad.net.entity.DeliveryContainerEntity;

import java.util.List;

/**
 * Created by Administrator on 2018/12/5.
 */

public class DeliveryContainerListAdapter extends BaseAdapter {
    List<DeliveryContainerEntity.DeliveryBean> deliveries;
    Context context;
    LayoutInflater inflater;

    private int checked_position = -1;

    public DeliveryContainerListAdapter(List<DeliveryContainerEntity.DeliveryBean> deliveries, Context context) {
        this.deliveries = deliveries;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return deliveries.size();
    }

    @Override
    public Object getItem(int position) {
        return deliveries.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_delivery_list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        try {
            holder.req_store.setText(deliveries.get(position).getNAME());
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            holder.bill_code.setText(deliveries.get(position).getBILL_CODE());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (checked_position == position) {
            holder.bg.setBackgroundColor(Color.WHITE);
        } else {
            holder.bg.setBackgroundColor(context.getResources().getColor(R.color.color_f0f0f0));
        }
        return convertView;
    }

    public static class ViewHolder {
        TextView req_store;
        TextView bill_code;
        LinearLayout bg;

        public ViewHolder(View view) {
            req_store = view.findViewById(R.id.item_delivery_list_req_store);
            bill_code = view.findViewById(R.id.item_delivery_list_bill_code);
            bg = view.findViewById(R.id.item_delivery_list_linear);
        }
    }

    public void setChecked_position(int checked_position) {
        this.checked_position = checked_position;
        notifyDataSetChanged();
    }

    public int getChecked_position() {
        return checked_position;
    }
}
